package com.caocaog.market.data.transfer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.data.transfer.entity.*;
import com.caocaog.market.data.transfer.mapper.MkSysUserMapper;
import com.caocaog.market.data.transfer.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.system.vo.LoginUserSite;
import org.jeecg.common.system.vo.LoginUserSiteArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author liguang
 * @version 1.0.0
 * @Project jeecg-boot
 * @Package com.caocaog.market.data.transfer.controller
 * @ClassName DataTransferController.java
 * @createTime 2021年11月16日 10:55:00
 * @Description TODO
 */
@Api(tags = "数据迁移")
@RestController
@RequestMapping("/dataTransfer/site")
@Slf4j
@SuppressWarnings("all")
public class DataTransferController{
    @Autowired
    private CcAgentService  ccAgentService;
    @Autowired
    private MkSiteBasicService mkSiteBasicService;
    @Autowired
    private CcAgentAreaToLeadService ccAgentAreaToLeadService;
    @Autowired
    private MkSiteAreaService mkSiteAreaService;
    @Autowired
    private CcUserService ccUserService;
    @Autowired
    private MkSysUserService mkSysUserService;
    @Autowired
    private CommonAPI commonAPI;

    @AutoLog("分站数据——“曹操跑腿”系统代理商列表数据查询")
    @ApiOperation(value = "分站数据-“曹操跑腿”系统代理商列表数据查询", notes = "分站数据-“曹操跑腿”系统代理商列表数据查询")
    @RequestMapping(value = "/agentList", method = RequestMethod.GET)
    public Result<IPage<CcAgent>> queryAgentPageList(CcAgent agents, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                @RequestParam(name="pageSize", defaultValue="100") Integer pageSize, HttpServletRequest req) {
        LoginUser currentUser =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LoginUserSite loginUserSite = commonAPI.getSiteByName(currentUser.getUsername());
        List<LoginUserSiteArea> loginUserSiteAreas = commonAPI.getSiteAreaByName(currentUser.getUsername());
        Result<IPage<CcAgent>> result = new Result<IPage<CcAgent>>();
        Page<CcAgent> page = new Page<CcAgent>(pageNo, pageSize);
        IPage<CcAgent> pageList = ccAgentService.findAllSiteByOriginal(page,agents);
        log.info("查询当前页："+pageList.getCurrent());
        log.info("查询当前页数量："+pageList.getSize());
        log.info("查询结果数量："+pageList.getRecords().size());
        log.info("数据总数："+pageList.getTotal());
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }
    @AutoLog(value="分站数据-“曹操来了”系统代理商列表数据查询")
    @ApiOperation(value = "分站数据-“曹操来了”系统列表数据查询", notes = "分站数据-“曹操来了”系统列表数据查询")
    @RequestMapping(value = "/siteList", method = RequestMethod.GET)
    public Result<IPage<MkSiteBasic>> querySitePageList(MkSiteBasic siteBasic, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                    @RequestParam(name="pageSize", defaultValue="100") Integer pageSize, HttpServletRequest req) {
        Result<IPage<MkSiteBasic>> result = new Result<IPage<MkSiteBasic>>();
        Page<MkSiteBasic> page = new Page<MkSiteBasic>(pageNo, pageSize);
        IPage<MkSiteBasic> pageList = mkSiteBasicService.findAllSiteByNow(page,siteBasic);
        log.info("查询当前页："+pageList.getCurrent());
        log.info("查询当前页数量："+pageList.getSize());
        log.info("查询结果数量："+pageList.getRecords().size());
        log.info("数据总数："+pageList.getTotal());
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }
    @AutoLog("分站数据——数据转换")
    @ApiOperation(value = "分站数据-数据转换", notes = "分站数据-sqlServer库中的cc_agent表  向mysql库中的mk_site_basic表中转数据")
    @RequestMapping(value = "/transfer", method = RequestMethod.POST)
    // 执行这个方法的时候，清除redis中的分站数据的缓存
    @CacheEvict(cacheNames= CacheConstant.SYS_ALL_SITE_CACHE)
    public Result transferAgentToSite(HttpServletRequest req) {
        LoginUser currentUser =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<CcAgent> agentList = ccAgentService.findAllSiteForNoPage(null);
        String relt = mkSiteBasicService.insertForReplace(agentList);
        Result result = new Result();
        result.setResult(relt);
        result.setSuccess(true);
        return result;
    }
    @AutoLog("分站区域——“曹操跑腿”系统内的分站对应区域信息")
    @ApiOperation(value = "分站区域-“曹操跑腿”系统代理商关联的区域数据列表信息", notes = "分站区域-“曹操跑腿”系统代理商关联的区域数据列表信息")
    @RequestMapping(value = "/agentArea/list", method = RequestMethod.GET)
    public Result<IPage<CcAgentAreaToLead>> queryAgentAreaPageList(CcAgentAreaToLead ccAgentAreaToLead, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                               @RequestParam(name="pageSize", defaultValue="100") Integer pageSize, HttpServletRequest req) {
        Result<IPage<CcAgentAreaToLead>> result = new Result<IPage<CcAgentAreaToLead>>();
        Page<CcAgentAreaToLead> page = new Page<CcAgentAreaToLead>(pageNo, pageSize);
        IPage<CcAgentAreaToLead> pageList = ccAgentAreaToLeadService.findAllSiteAreaByOriginal(page,ccAgentAreaToLead);
        log.info("查询当前页："+pageList.getCurrent());
        log.info("查询当前页数量："+pageList.getSize());
        log.info("查询结果数量："+pageList.getRecords().size());
        log.info("数据总数："+pageList.getTotal());
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @AutoLog("分站区域——“曹操来了”系统内的分站对应区域信息")
    @ApiOperation(value = "分站区域-“曹操来了”系统代理商关联的区域数据列表信息", notes = "分站区域-“曹操来了”系统代理商关联的区域数据列表信息")
    @RequestMapping(value = "/siteArea/list", method = RequestMethod.GET)
    public Result<IPage<MkSiteArea>> querySiteAreaPageList(MkSiteArea mkSiteArea, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                  @RequestParam(name="pageSize", defaultValue="100") Integer pageSize, HttpServletRequest req) {
        Result<IPage<MkSiteArea>> result = new Result<IPage<MkSiteArea>>();
        Page<MkSiteArea> page = new Page<MkSiteArea>(pageNo, pageSize);
        page.setTotal(mkSiteAreaService.countForfindAllSiteAreaByNow(mkSiteArea));
        page.setSearchCount(false); // 不进行count数量查询
        IPage<MkSiteArea> pageList = mkSiteAreaService.findAllSiteAreaByNow(page,mkSiteArea);
        log.info("查询当前页："+pageList.getCurrent());
        log.info("查询当前页数量："+pageList.getSize());
        log.info("查询结果数量："+pageList.getRecords().size());
        log.info("数据总数："+pageList.getTotal());
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @AutoLog("分站区域——数据转换")
    @ApiOperation(value = "分站区域-数据转换", notes = "分站区域-sqlServer库中的cc_agent_area_tolead表  向mysql库中的mk_site_area表中转数据")
    @RequestMapping(value = "/siteArea/transfer", method = RequestMethod.POST)
    public Result transferAgentAreaToSiteArea(@RequestBody MkSiteBasic mkSiteBasic, HttpServletRequest req) {

        Result result = new Result();
        long time1 = System.currentTimeMillis();
        List<MkSiteBasic> siteBasicList = mkSiteBasicService.findAllSiteForNoPage(mkSiteBasic);
        String relt = mkSiteAreaService.insertSiteAreaForInsert(siteBasicList);
        System.out.println("relt=====   "+relt);
        long time2 = System.currentTimeMillis();
        System.out.println("总耗时："+(time2-time1));
        result.setResult(relt);
        result.setSuccess(true);
        return result;
    }

    @AutoLog("分站用户——“曹操跑腿”系统内的分站户信息")
    @ApiOperation(value = "分站用户-“曹操跑腿”系统代理商用户数据列表信息", notes = "分站用户-“曹操跑腿”系统代理商用户数据列表信息")
    @RequestMapping(value = "/agentUser/list", method = RequestMethod.GET)
    public Result<IPage<CcUser>> queryAgentAreaPageList(CcUser ccUser, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                        @RequestParam(name="pageSize", defaultValue="100") Integer pageSize, HttpServletRequest req) {
        Result<IPage<CcUser>> result = new Result<IPage<CcUser>>();
        Page<CcUser> page = new Page<CcUser>(pageNo, pageSize);
        page.setSearchCount(false);
        page.setTotal(ccUserService.countForfindAllUserByOriginal(ccUser));
        IPage<CcUser> pageList = ccUserService.findAllUserByOriginal(page,ccUser);
        log.info("agentUser/list-----》查询当前页："+pageList.getCurrent());
        log.info("agentUser/list-----》查询当前页数量："+pageList.getSize());
        log.info("agentUser/list-----》查询结果数量："+pageList.getRecords().size());
        log.info("agentUser/list-----》数据总数："+pageList.getTotal());
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }
    @AutoLog("分站用户——“曹操来了”系统内的分站用户信息")
    @ApiOperation(value = "分站用户-“曹操来了”系统代理商用户数据列表信息", notes = "分站用户-“曹操来了”系统代理商用户数据列表信息")
    @RequestMapping(value = "/siteUser/list", method = RequestMethod.GET)
    public Result<IPage<MkSysUser>> queryAgentAreaPageList(MkSysUser mkSysUser, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                        @RequestParam(name="pageSize", defaultValue="100") Integer pageSize, HttpServletRequest req) {
        Result<IPage<MkSysUser>> result = new Result<IPage<MkSysUser>>();
        Page<MkSysUser> page = new Page<MkSysUser>(pageNo, pageSize);
        mkSysUser.setUserType(2); // 查代理商用户
        IPage<MkSysUser> pageList = mkSysUserService.findAllSysUserByNow(page,mkSysUser);
        log.info("查询当前页："+pageList.getCurrent());
        log.info("查询当前页数量："+pageList.getSize());
        log.info("查询结果数量："+pageList.getRecords().size());
        log.info("数据总数："+pageList.getTotal());
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @AutoLog("分站用户——数据转换")
    @ApiOperation(value = "分站用户-数据转换", notes = "分站用户-sqlServer库中的cc_user表  向mysql库中的sys_user表中转数据")
    @RequestMapping(value = "/siteUser/transfer", method = RequestMethod.POST)
    public Result transferAgentUserToSiteUser(HttpServletRequest req) {
        Result result = new Result();
        long time1 = System.currentTimeMillis();
        CcUser ccUser= new CcUser();
        List<CcUser> ccUserList = ccUserService.findAllUserForNoPage(null);
        String relt = mkSysUserService.insertSysUserForReplace(ccUserList);
        System.out.println("relt=====   "+relt);
        long time2 = System.currentTimeMillis();
        System.out.println("分站用户转移 总耗时："+(time2-time1));
        result.setResult(relt);
        result.setSuccess(true);
        return result;
    }

}
